let score=0;
    export default class UICtrl extends Laya.Script {

    constructor() { 
        super(); 
        /** @prop {name:txt_score, tips:"数字类型示例", type:Node, default:null}*/
        this.txt_score=null;
        /** @prop {name:gameoverPanel, tips:"数字类型示例", type:Node, default:null}*/
        this.gameoverPanel=null;
        /** @prop {name:rankPanel, tips:"数字类型示例", type:Node, default:null}*/
        this.rankPanel=null;
        /** @prop {name:txt_Start, tips:"数字类型示例", type:Node, default:null}*/
        this.txt_Start=null;
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
    onAwake(){
        this.rankPane.visible=false;
        Laya.stage.on("AddScore",this,this.addScore);
        Laya.stage.on("Gameover",this,this.gameover);
        this.gameoverPanel.visible=false;

        this.init();
    }
    init(){
        this.gameoverPanel.getChildByName("btn_Again").on(Laya.Event.CLICK,this,this.btnAgainClick);
        this.gameoverPanel.getChildByName("btn_Rank").on(Laya.Event.CLICK,this,this.btnRankClick);
        this.txt_Rank=this.rankPanel.getChildByName("txt_Rank");
        this.txt_score.text="分数:0";
        this.txt_score.visible=false;
        Laya.stage.on(Laya.Event.CLICK,this,function(){
            this.txt_Start.visible=false;
            Laya.stage.event("Start");
            this.txt_score.visible=true;
        })
    }
    addScore(){
        score++;
        this.txt_score.text="分数:"+score;

    }
    gameover(){
        this.txt_score.visible=false;
         Laya.Tween.to(this.gameoverPanel,{alpha:1},500,Laya.Ease.linearIn);
        this.gameoverPanel.visible=true;
       
    }
    btnAgainClick(){
        score=0;
        this.txt_score.text="分数:"+score;
        this.txt_score.visible=true;
        this.gameoverPanel.visible=false;
        Laya.stage.event("Again");
    }
    btnRankClick(){
        this.rankPane.visible=true;
        this.rankPane.show(true,true);
        // Laya.LocalStorage.clear();//清除本地排行榜信息
        var one=Number(Laya.LocalStorage.getItem("One"));
        var tow=Number(Laya.LocalStorage.getItem("Tow"));
        var three=Number(Laya.LocalStorage.getItem("Three"));

        var scoreArr=[];
        scoreArr.push(one,tow,three,score);
        scoreArr=this.bubbleSort(scoreArr);
        Laya.LocalStorage.setItem("One",scoreArr[0])
        Laya.LocalStorage.setItem("Tow",scoreArr[1])
        Laya.LocalStorage.setItem("Three",scoreArr[2])

        this.txt_Rank.text=" 1 - "+scoreArr[0]+"\n 2 - "+scoreArr[1]+"\n 3 - "+scoreArr[2]
    }
    bubbleSort(arr){
        var len=arr.length;
        for(var i=0;i<len;i++){
            for(var j=0;j<len-i-1;j++){
                if(arr[j]<arr[j+1]){
                    var temp=arr[j+1];
                    arr[j+1]=arr[j];
                    arr[j]=temp;
                }
            }
        }
        return arr;
    }
}